<?php
namespace app\service;
use think\Db;
use invoice\InvoiceApi;
class invoiceService{
    public function invoiceInfo($taxnum){
        return Db::table("service_invoice")->where("fp_saler_tax_num",$taxnum)->find();
    }
    public function parkingInvoice($parkingNo){
        $propertyinfo=Db::table("community")
                        ->alias("c")
                        ->join(["parkinglotinfo"=>"a"],"c.id=a.community_id")
                        ->join(["property"=>"p"],"c.property_id=p.id")
                        ->where("a.parkingNo",$parkingNo)
                        ->field("p.service_id")
                        ->find();
        $invoiceinfo=Db::table("service_invoice")
                    ->alias("a")
                    ->join(["service"=>"b"],"a.service_id=b.id")
                    ->where("a.service_id",$propertyinfo["service_id"])
                    ->field("a.*,b.name,b.address,b.phone")
                    ->find();
        
        return $invoiceinfo;
        
        
    }
    
    public function getToken($service_id){
        $serviceinfo=Db::table("service_invoice")->where("service_id",$service_id)->find();
        $refreshToken = $serviceinfo["fp_refresh_token"];
		$appSecret = "B69D206D6E564A6E";
		$userId = $serviceinfo["userId"];
		$res = InvoiceApi::refreshISVToken($refreshToken, $userId, $appSecret);
		
		$res=json_decode($res,true);
        if(array_key_exists("error",$res)){
            return $res;
            //return $res["error_description"];
        }else{
            $info["fp_expires_in"]=date("Y-m-d H:i:s");
            $info["fp_access_token"]=$res["access_token"];
            $info["fp_refresh_token"]=$res["refresh_token"];
            
            $re=Db::table("service_invoice")->where("service_id",$serviceinfo["service_id"])->update($info);
            return $info;
        }
		
		
    }
    public function getISVToken($appKey,$appSecret,$code,$taxnum,$redirectUri="") {
		
		$res = InvoiceApi::getISVToken($appKey, $appSecret, $code, $taxnum, $redirectUri);
		return $res;
	}

	public function refreshISVToken() {
		$refreshToken = "your.token";
		$appSecret = "your.appSecret";
		$userId = "your.userId";
		$res = InvoiceApi::refreshISVToken($refreshToken, $userId, $appSecret);
		return $res;
	}
}
